//
//  VCOfClickButtonInListenFM.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/3.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfClickButtonInListenFM.h"
#import "BaseTableView.h"
#import "MJMacro.h"
#import "TVCellOfSecondRowListenFM.h"
#import "ZWYNetTool.h"
#import "ClickButtonInListenFMRootClass.h"
#import "ClickButtonInListenFMData.h"
#import "UIImageView+WebCache.h"
#import "VCOfListenFMPlayInterface.h"
#import "MJRefresh.h"
@interface VCOfClickButtonInListenFM ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) ClickButtonInListenFMRootClass *clickButtonInListenFMRootClass;
@property (nonatomic, strong) BaseTableView *tableViewOfClickButtonInListenFM;
@property (nonatomic, strong) TVCellOfSecondRowListenFM *cell;
@property (nonatomic, strong) NSMutableArray<ClickButtonInListenFMData *> *mArrayOfData;
@property (nonatomic, strong) NSMutableArray<ClickButtonInListenFMData *> *mArrayOfClickButtonInListenFMData;
@property (nonatomic, assign) int numberOfOffect;
@end

@implementation VCOfClickButtonInListenFM

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    _numberOfOffect = 10;
    // 创建tableView
    [self createTableView];
    // 数据解析
    [self handleData];
}
#pragma mark - 数据解析
- (void)handleData{
    [ZWYNetTool GET:_stringOfURL andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _clickButtonInListenFMRootClass = [ClickButtonInListenFMRootClass modelDic:result];
        _mArrayOfData = _clickButtonInListenFMRootClass.data.mutableCopy;
        _mArrayOfClickButtonInListenFMData = _clickButtonInListenFMRootClass.data.mutableCopy;
        [self.tableViewOfClickButtonInListenFM reloadData];
    } andFailureBlock:^(NSError *error) {
    }];
    [self keepHandleData];
}
#pragma mark -
- (void)keepHandleData{
    NSString *stringURl = [_stringOfURL stringByReplacingOccurrencesOfString:@"offset=0" withString:@"offset=%d"];
    [ZWYNetTool GET:[NSString stringWithFormat:stringURl, _numberOfOffect] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        //        _clickButtonInListenFMRootClassOfHOt = []
        if ([result[@"data"] lastObject][@"title"]) {
            NSLog(@"存在");
            static int i = 0;
            i++;
            NSLog(@"%d", i);
            NSLog(@"%@", [result[@"data"] lastObject][@"title"]);
            _clickButtonInListenFMRootClass = [ClickButtonInListenFMRootClass modelDic:result];
            for (ClickButtonInListenFMData *clickButtonInListenFMData in _clickButtonInListenFMRootClass.data) {
                [_mArrayOfClickButtonInListenFMData addObject:clickButtonInListenFMData];
                NSLog(@"%@", clickButtonInListenFMData.title);
            }
            _numberOfOffect = _numberOfOffect + 10;
            
            [self keepHandleData];
        }
        [self.tableViewOfClickButtonInListenFM reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableViewOfClickButtonInListenFM = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_tableViewOfClickButtonInListenFM];
    self.tableViewOfClickButtonInListenFM.delegate = self;
    self.tableViewOfClickButtonInListenFM.dataSource = self;
    // 这个界面的 cell 和 之前的界面有个部分一样, 直接用了
    [self.tableViewOfClickButtonInListenFM registerNib:[UINib nibWithNibName:@"TVCellOfSecondRowListenFM" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfSecondRowListenFM"];
    // 下拉刷新
    self.tableViewOfClickButtonInListenFM.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 上拉加载
    self.tableViewOfClickButtonInListenFM.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
#pragma mark - 上拉刷新
- (void)loadNewData{
    [self handleData];
    [self.tableViewOfClickButtonInListenFM.mj_header endRefreshing];
}
#pragma mark - 下拉加载
- (void)loadMoreData{
    static int i = 5;
    NSString *stringURl = [_stringOfURL stringByReplacingOccurrencesOfString:@"offset=0" withString:@"offset=%d"];
    [ZWYNetTool GET:[NSString stringWithFormat:stringURl, i] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _clickButtonInListenFMRootClass = [ClickButtonInListenFMRootClass modelDic:result];
        _mArrayOfData = _clickButtonInListenFMRootClass.data.mutableCopy;
        for (ClickButtonInListenFMData *clickButtonInListenFMData in _clickButtonInListenFMRootClass.data) {
            [_mArrayOfData addObject:clickButtonInListenFMData];
        }
        [self.tableViewOfClickButtonInListenFM reloadData];
    } andFailureBlock:^(NSError *error) {
    }];
    i = i + 5;
    [self.tableViewOfClickButtonInListenFM.mj_footer endRefreshing];
}
#pragma mark - tableView 代理方法 row 数量的确定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mArrayOfData.count;
}
#pragma mark - tableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfSecondRowListenFM" forIndexPath:indexPath];
    [self.cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:_mArrayOfData[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
    self.cell.speakLabel.text = _mArrayOfData[indexPath.row].speak;
    self.cell.titleLabel.text = _mArrayOfData[indexPath.row].title;
    self.cell.viewnumLabel.text = _mArrayOfData[indexPath.row].viewnum;
    return self.cell;
}
#pragma mark - row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT * 0.15;
}
#pragma mark - tableView cell 的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VCOfListenFMPlayInterface *vcOfListenFMPlayInterface = [VCOfListenFMPlayInterface defaultManager];
    // 歌曲下一个角标的确定
    vcOfListenFMPlayInterface.numberOfSelectNextSong = indexPath.row + 1;
    // 歌曲下一个角标的确定
    vcOfListenFMPlayInterface.numberOfSelectLastSong = indexPath.row - 1;
    // 传递 model
//    vcOfListenFMPlayInterface.clickButtonInListenFMRootClass = _clickButtonInListenFMRootClass;
    vcOfListenFMPlayInterface.mArrayOfClickButtonInListenFMData = _mArrayOfClickButtonInListenFMData;
    vcOfListenFMPlayInterface.urlString = _mArrayOfData[indexPath.row].url;
    vcOfListenFMPlayInterface.coverString = _mArrayOfData[indexPath.row].cover;
    vcOfListenFMPlayInterface.titleString = _mArrayOfData[indexPath.row].title;
    vcOfListenFMPlayInterface.speakString = _mArrayOfData[indexPath.row].speak;
    [self presentViewController:vcOfListenFMPlayInterface animated:YES completion:^{
    }];
}
#pragma mark - 利用GCD的dispatch_once的方法实现单利模式
+ (instancetype)defaultManager
{
    static VCOfClickButtonInListenFM *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
#pragma mark - 其他方法, 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
